<?php
define('KE', 1);
define('IN_SCRIPT', 1);
define('HESK_PATH', '../');
define('HESK_NO_ROBOTS', 1);

/* Get all the required files and functions */
require(HESK_PATH . 'hesk_settings.inc.php');
require(HESK_PATH . 'inc/common.inc.php');
require(HESK_PATH . 'inc/database.inc.php');

hesk_session_start();
hesk_dbConnect();

if (!$_SESSION['isadmin']) {
    exit();
}

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $link = $_POST['link'];
    $start = $_POST['start'];
    $end = $_POST['end'];
    $id = $_POST['id'];

    if ($id) {
        $sql = "UPDATE hesk_ads SET name = '$name', link = '$link', start = '$start', end = '$end' WHERE id = $id";
        hesk_dbQuery($sql);
    } else {
        $sql = "INSERT INTO hesk_ads(id, name, link, start, end) VALUES (NULL, '$name', '$link', '$start', '$end')";
        hesk_dbQuery($sql);
        $id = hesk_dbInsertID();
    }
    
    $move = move_uploaded_file($_FILES['image']['tmp_name'], '../img/ads/' . $id . '.gif');

    header("Location: ae.php");
}

    if (isset($_GET['id'])) {
        $id = $_GET['id'];

        $sql = "SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "ads` WHERE id = $id";
        $result = hesk_dbQuery($sql);

        $row = hesk_dbFetchAssoc($result);

        $name = $row['name'];
        $link = $row['link'];
        $start = $row['start'];
        $end = $row['end'];
    }


    /* Print header */
    require_once(HESK_PATH . 'inc/header.admin.inc.php');

    /* Print admin navigation */
    require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');
    ?>
    <br />

    <form name="ae_form" action="ae_add.php" method="post" enctype="multipart/form-data">
        <table width="100%" border="1" cellspacing="0" cellpadding="3">
            <tr>
                <td>Nama Iklan</td>
                <td><input type="text" style="width: 40%" name="name" value="<?php echo $name; ?>"></input></td>
            </tr>
            <tr>
                <td>Link</td>
                <td><input type="text" style="width: 90%" name="link" value="<?php echo $link; ?>"></input></td>
            </tr>
            <tr>
                <td>Tanggal Mulai (YYYY-MM-DD)</td>
                <td><input type="text" name="start" value="<?php echo $start; ?>"></input></td>
            </tr>
            <tr>
                <td>Tanggal Selesai (YYYY-MM-DD)</td>
                <td><input type="text" name="end" value="<?php echo $end; ?>"></input></td>
            </tr>
            <tr>
                <td>Gambar</td>
                <td><input type="file" style="width: 40%" name="image"></input></td>
            </tr>
        </table>
        <br />
        <input type="hidden" name="id" value="<?php echo $id; ?>">
        <input type="submit" name="submit" value="Simpan"></input>
    </form>

    <?php
    hesk_dbClose();
    
define('HIDE_ADS', 1);
    require_once(HESK_PATH . 'inc/footer.inc.php');
    ?>
